import java.io.*;
import java.util.*;

public class SuperDir {

	static String dir1="";
	static String dir2="";
	static boolean echo=false; 
	static boolean ignorefilename=false;
	static int mismatch=111111;
	public static void main(String[] args) throws Exception{
		if(args.length<2){
			System.out.println("#need 2 args");
			System.exit(0);
		}
		if(args.length>2 && args[2].startsWith("t"))
			ignorefilename=true;
		if(args.length>2)
			echo=true;
		File f1= new File(args[0]);
		File f2= new File(args[1]);
		if(!f1.exists() || !f2.exists()){
			__print("#dosn't exist");
			System.exit(0);
		}
		if(f1.isDirectory() && f2.isDirectory()){
			int result=compareDir(f1, f2);
			__print(result+"");
			if(result==1000)
				__print(f1.getAbsolutePath()+">>"+f2.getAbsolutePath());
			if(result==-1000)
				__print(f2.getAbsolutePath()+">>"+f1.getAbsolutePath());
			if(result==0)
				__print(f2.getAbsolutePath()+"=="+f1.getAbsolutePath());
		}else
			__print("#wrong compare.");
			
	}

	static void __print(String str){
		if(echo){
			System.out.println(str);
		}
	}

	static  int compareDir(File f1, File f2) throws Exception{
		String p1=f1.getAbsolutePath();
		String p2=f2.getAbsolutePath();
		File[] farr1=f1.listFiles();
		File[] farr2=f2.listFiles();
		TreeMap<String,File> fls1= new TreeMap<String,File>();
		TreeMap<String,File> fls2= new TreeMap<String,File>();
		for(File _f1:farr1){
			fls1.put(_f1.getName(),_f1);
		}
		for(File _f2:farr2){
			fls2.put(_f2.getName(),_f2);
		}
		ArrayList<String> keys1=new ArrayList<String>();
		ArrayList<String> keys11=new ArrayList<String>();
		keys1.addAll(fls1.keySet());
		keys11.addAll(fls1.keySet());
		ArrayList<String> keys2=new ArrayList<String>();
		ArrayList<String> keys22=new ArrayList<String>();
		keys2.addAll(fls2.keySet());
		keys22.addAll(fls2.keySet());
		int m=keys1.size();
		int n=keys2.size();
		int ret=0;
		int max=n;
		if(m >n){
			ret=1000;
			keys11.removeAll(keys22);
			for(String _key11:keys11){
				__print(""+p1+"/"+_key11 +" > "+p2+"/");
			}
			for(int j=0;j<n;j++){
				String _name=keys2.get(j);
				if(!keys1.contains(_name)){
					__print(""+p1+"/  < "+p2+"/"+_name);
					return mismatch+1;
				}

				File _1=fls1.get(_name);
				File _2=fls2.get(_name);
				if(_1.isDirectory() && _2.isDirectory()){
					int result=compareDir(_1, _2);
					if(result==0 || result ==1000){
						continue;
					}else{
						if(result==-1000)
							__print(p1+"/"+_name+" < "+p2+"/"+_name);
						else
							__print(p1+"/"+_name+"!!="+p2+"/"+_name);
						return mismatch+3;
					}
				}
				else if(_1.isFile() && _2.isFile()){
					if( !dif.comp(new String[]{_1.getAbsolutePath(), _2.getAbsolutePath()})){
						__print(p1+"/"+_name+"!="+p2+"/"+_name);
						return mismatch+4;
					}
				}else{
					__print("ERROR: File Vs Dir"+p1+"/"+_name+"!="+p2+"/"+_name);
					return mismatch+5;
				}
					
			}

		}else if(m < n){
			ret=-1000;
			keys22.removeAll(keys11);
			for(String _key22:keys22){
				__print(""+p1+"/"+" < "+p2+"/"+_key22);
			}
			for(int i=0;i<m;i++){
				String _name=keys1.get(i);
				if(!keys2.contains(_name)){
					__print(""+p1+"/  > "+p2+"/"+_name);
					return mismatch+11;
				}

				File _1=fls1.get(_name);
				File _2=fls2.get(_name);
				if(_1.isDirectory() && _2.isDirectory()){
					int result=compareDir(_1, _2);
					if(result==0 || result ==-1000){
						continue;
					}else{
						if(result==1000)
							__print(p1+"/"+_name+" > "+p2+"/"+_name);
						else
							__print(p1+"/"+_name+"!!="+p2+"/"+_name);
						return mismatch+13;
					}
				}
				else if(_1.isFile() && _2.isFile()){
					if( !dif.comp(new String[]{_1.getAbsolutePath(), _2.getAbsolutePath()})){
						__print(p1+"/"+_name+"!="+p2+"/"+_name);
						return mismatch+14;
					}
				}else{
					__print("ERROR: File Vs Dir"+p1+"/"+_name+"!="+p2+"/"+_name);
					return mismatch+15;
				}
					
			}

			
		}else{
			ret=0;
			for(int i=0,j=0;j<max;i++,j++){
				if(keys1.get(i).compareTo(keys2.get(j)) != 0 ){
					__print(keys1.get(i)+"!="+keys2.get(j));
					return mismatch+21;
				}

				File _1=fls1.get(keys1.get(i));
				File _2=fls2.get(keys2.get(j));
				if(_1.isDirectory() && _2.isDirectory()){
					int result=compareDir(_1, _2);
					if(ret==0 && (result==0 || result==1000 ||result==-1000)){
						ret=result;
						continue;
					}else if(result==0 || result==ret){
						continue;
					}else{
						__print(p1+"/"+keys1.get(i)+"!!="+p2+"/"+keys2.get(j));
						return mismatch+22;
					}
				}
				else if(_1.isFile() && _2.isFile()){
					if( !dif.comp(new String[]{_1.getAbsolutePath(), _2.getAbsolutePath()})){
						__print(_1.getAbsolutePath()+"!="+_2.getAbsolutePath());
						return mismatch+23;
					}
				}else{
					__print("ERROR: File Vs Dir"+_1.getAbsolutePath()+"!="+_2.getAbsolutePath());
					return mismatch+24;
				}
					
			}
			
			
		}
		return ret;
		
	}

}
